package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.LeaveRequestForm;
import com.ruoyi.business.service.LeaveRequestFormService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author danghf
 * @version 1.0
 * @date 2024/05/06 9:43
 * 请假流程
 */
@RestController
@RequestMapping("/leave")
public class LeaveController extends BaseController {

    private final LeaveRequestFormService leaveRequestFormService;
    /**
     * 仓库管理
     */
    private final RepositoryService repositoryService;


    public LeaveController(LeaveRequestFormService leaveRequestFormService, RepositoryService repositoryService) {
        this.leaveRequestFormService = leaveRequestFormService;
        this.repositoryService = repositoryService;
    }

    public final String PROCESS_DEFINITION_KEY = "leave";
    /**
     * 发起流程
     * @param map 流程参数(包含表单对象)
     * @return ajaxResult
     */
    @PostMapping("/start")
    public AjaxResult start(@RequestBody Map<String,Object> map){
        // 获取请假流程
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(PROCESS_DEFINITION_KEY)
                .latestVersion()
                .singleResult();
        if (processDefinition == null) {
            return AjaxResult.error("请假流程未部署");
        }else {
           return leaveRequestFormService.start(LeaveRequestForm.buildByMap((Map)map.get("form")) ,(Map<String, Object>)map.get("map"),processDefinition);
        }
    }

    /**
     * 获取请假表单
     */
    @GetMapping("/getFormById")
    public LeaveRequestForm getFormById(Long id){
        return leaveRequestFormService.selectLeaveRequestFormById(id);
    }

}
